package com.kedou.net;

import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import com.kedou.app.Config;
import com.kedou.app.R;
import com.kedou.domain.ProjectItem;
import com.kedou.domain.User;
import com.kedou.net.PostFollowedProject.FailCallback;
import com.kedou.net.PostFollowedProject.SuccessCallback;
import com.kedou.util.Util;

/**
 * 后台请求：获取用户信息
 * 
 * @author zhouchao
 * 
 */
public class PostUserInfo {
	public PostUserInfo(String token, String userId, String os,
			String deviceId, final SuccessCallback successCallback,
			final FailCallback failCallback, final Context context) {
		new NetConnection(
				Util.getSDBaseUrl() + Config.URL_GETUSERINFO,
				HttpMethod.POST,
				new NetConnection.SuccessCallback() {

					@Override
					public void onSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							JSONObject dataObject = obj
									.getJSONObject(Config.KEY_DATA);
							switch (obj.getInt(Config.KEY_STATUSCODE)) {
							case Config.RESULT_STATUS_SUCCESS:
								User user = new User();
								if (dataObject.has("portrait")) {
									user.setPortrait(dataObject
											.getString("portrait"));
								}
								if (dataObject.has("realName")) {
									user.setRealName(dataObject
											.getString("realName"));
								}
								if (dataObject.has("email")) {
									user.setEmail(dataObject.getString("email"));
								}
								if (dataObject.has("certStatus")) {
									user.setCertStatus(dataObject
											.getInt("certStatus"));
								}
								if (dataObject.has("IDCard")) {
									user.setIDCard(dataObject
											.getString("IDCard"));
								}
								if (dataObject.has("phoneNum")) {
									user.setPhoneNum(dataObject
											.getString("phoneNum"));
								}
								if (dataObject.has("weChat")) {
									user.setWeChat(dataObject
											.getString("weChat"));
								}
								if (dataObject.has("nickName")) {
									user.setNickName(dataObject
											.getString("nickName"));
								}
								if (dataObject.has("myWealth")) {
									user.setMyWealth(dataObject
											.getString("myWealth"));
								}
								if (dataObject.has("kedouNum")) {
									user.setKedouNum(dataObject
											.getInt("kedouNum"));
								}
								if (dataObject.has("fansNum")) {
									user.setFansNum(dataObject
											.getInt("fansNum"));
								}
								if (successCallback != null) {
									successCallback.onSuccess(
											obj.getString(Config.KEY_STATUSMSG),
											user);
								}
								break;
							default:
								if (failCallback != null) {
									failCallback.onFail(obj
											.getString(Config.KEY_STATUSMSG));
								}
								break;
							}

						} catch (JSONException e) {
							e.printStackTrace();
							if (failCallback != null) {
								failCallback.onFail(context.getResources()
										.getString(R.string.server_have_wrong));
							}
						}
					}
				}, new NetConnection.FailCallback() {

					@Override
					public void onFail() {
						if (failCallback != null) {
							failCallback.onFail(context.getResources()
									.getString(R.string.server_have_wrong));
						}
					}
				}, Config.KEY_TOKEN, token, Config.KEY_USERID, userId,
				Config.KEY_OS, os, Config.KEY_DEVICE, deviceId);
	}

	public static interface SuccessCallback {
		void onSuccess(String msg, User mUser);
	}

	public static interface FailCallback {
		void onFail(String msg);
	}
}
